package com.dji.controller;

import com.dji.dao.DroneDao;
import com.dji.model.Drone;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

/**
 * 提供无人机实时数据的API接口
 */
public class DroneDataServlet extends HttpServlet {
    private DroneDao droneDao = new DroneDao();
    private Random random = new Random();
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String droneId = request.getParameter("droneId");
        
        if (droneId != null && !droneId.isEmpty()) {
            Drone drone = droneDao.getDroneById(droneId);
            
            if (drone != null) {
                // 模拟无人机数据变化
                updateDroneData(drone);
                droneDao.saveDrone(drone);
                
                // 返回无人机数据
                JSONObject jsonDrone = new JSONObject();
                jsonDrone.put("id", drone.getId());
                jsonDrone.put("model", drone.getModel());
                jsonDrone.put("batteryLevel", drone.getBatteryLevel());
                jsonDrone.put("latitude", drone.getLatitude());
                jsonDrone.put("longitude", drone.getLongitude());
                jsonDrone.put("altitude", drone.getAltitude());
                jsonDrone.put("speed", drone.getSpeed());
                jsonDrone.put("status", drone.getStatus());
                
                response.setContentType("application/json");
                response.setCharacterEncoding("UTF-8");
                PrintWriter out = response.getWriter();
                out.print(jsonDrone.toString());
                out.flush();
            } else {
                response.sendError(HttpServletResponse.SC_NOT_FOUND, "无人机未找到");
            }
        } else {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "缺少droneId参数");
        }
    }
    
    /**
     * 模拟无人机数据变化
     */
    private void updateDroneData(Drone drone) {
        if ("FLYING".equals(drone.getStatus()) || "RETURNING".equals(drone.getStatus())) {
            // 模拟电池消耗
            double newBatteryLevel = drone.getBatteryLevel() - (random.nextDouble() * 0.5);
            drone.setBatteryLevel(Math.max(0, newBatteryLevel));
            
            // 模拟位置变化
            if ("FLYING".equals(drone.getStatus())) {
                drone.setLatitude(drone.getLatitude() + (random.nextDouble() * 0.0002 - 0.0001));
                drone.setLongitude(drone.getLongitude() + (random.nextDouble() * 0.0002 - 0.0001));
                drone.setAltitude(drone.getAltitude() + (random.nextDouble() * 2 - 1));
                drone.setSpeed(5 + random.nextDouble() * 10);
            } else if ("RETURNING".equals(drone.getStatus())) {
                // 模拟返航
                drone.setSpeed(10 + random.nextDouble() * 5);
            }
        } else if ("LANDING".equals(drone.getStatus())) {
            // 模拟降落
            if (drone.getAltitude() > 0) {
                drone.setAltitude(Math.max(0, drone.getAltitude() - 1));
                drone.setSpeed(Math.max(0, drone.getSpeed() - 0.5));
            } else {
                drone.setStatus("IDLE");
                drone.setSpeed(0);
            }
        } else if ("IDLE".equals(drone.getStatus())) {
            drone.setSpeed(0);
        }
    }
}